    //
//  ContactDetailViewController.m
//  SpendFunk
//
//  Created by KID on 5/7/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "ContactDetailViewController.h"
#import "SpendFunkAppDelegate.h"


@implementation ContactDetailViewController

// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.


// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
    [super loadView];
    [self initView];
    [self initNotification];
}

-(void)initView
{
    /// backgrounnd for find friends screen
	NSString *path = [[NSString alloc] initWithFormat:@"%@",IMG_BKGR_VIEW];
	UIImage *img = [UIImage imageNamed:path];
	UIImageView *imgvFFBkgr = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
	[imgvFFBkgr setImage:img];
	[self.view addSubview:imgvFFBkgr];
	[imgvFFBkgr setUserInteractionEnabled:YES];
	[imgvFFBkgr release];
	////	
	[path release];
	path = [[NSString alloc] initWithFormat:@"%@",IMG_HEADER];
	img = [UIImage imageNamed:path];
	UIImageView *imgvBar = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, img.size.width, img.size.height)];
	[imgvBar setImage:img];
	[self.view addSubview:imgvBar];
	[imgvBar setUserInteractionEnabled:YES];
	[imgvBar release];
	//back	
	[path release];
	path = [[NSString alloc] initWithFormat:@"%@",IMG_BTN_BACK];
	img = [UIImage imageNamed:path];	
	m_pBtnBack = [[UIButton alloc] initWithFrame:CGRectMake(5, 5, img.size.width, img.size.height)];
	[m_pBtnBack setBackgroundImage:img forState:UIControlStateNormal];
	[m_pBtnBack setBackgroundColor:[UIColor clearColor]];
	[m_pBtnBack setTitle:@"Back" forState:UIControlStateNormal];
	m_pBtnBack.titleLabel.textColor = [UIColor whiteColor];
	[m_pBtnBack addTarget:nil action:@selector(onClickBack) forControlEvents:UIControlEventTouchUpInside];
	[self.view addSubview:m_pBtnBack];
	[path release];
    
	path = [[NSString alloc] initWithFormat:@"%@",IMG_TEXT_INVITEFRIENDS];
	img = [UIImage imageNamed:path];
	UIImageView *imgvText = [[UIImageView alloc] initWithFrame:CGRectMake(320/2-img.size.width/2, 70, img.size.width, img.size.height)];
	[imgvText setImage:img];
	[self.view addSubview:imgvText];
	[imgvText setUserInteractionEnabled:YES];
	[imgvText release];
    
    m_pImgAvatar = [[UIImageView alloc] initWithFrame:CGRectMake(30, 110, 80, 80)];
    [self.view addSubview:m_pImgAvatar];
    [m_pImgAvatar release];
    
    m_pLblName = [[UILabel alloc] initWithFrame:CGRectMake(120, 95, 180, 70)];
    m_pLblName.textColor = [UIColor colorWithRed:37.0f/255 green:46.0f/255 blue:72.0f/255 alpha:1];
    m_pLblName.backgroundColor = [UIColor clearColor];
    [m_pLblName setTextAlignment:UITextAlignmentLeft];
    [m_pLblName setFont:[UIFont boldSystemFontOfSize:16]];
    [self.view addSubview:m_pLblName];
    [m_pLblName release];
    
	
	int nHRow = 46;
	m_pTableContact = [[UITableView alloc] initWithFrame:CGRectMake(10,200, 300, 4*nHRow+15) style:UITableViewStyleGrouped];
	m_pTableContact.delegate =self;
	m_pTableContact.dataSource = self;
	m_pTableContact.rowHeight = nHRow;
	[m_pTableContact setShowsVerticalScrollIndicator:NO];
	[m_pTableContact setBackgroundColor:[UIColor clearColor]];
	m_pTableContact.scrollEnabled = YES;
    [self.view addSubview:m_pTableContact];
    [m_pTableContact release];
}

-(void)initNotification
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setArray:) name:@"DetailContact" object:nil];
}

-(void)setArray:(NSNotification*)arr
{
    m_pDicDetailContact = [arr object];
    
    m_pImgAvatar.image = [m_pDicDetailContact objectForKey:@"kImage"];
    [m_pLblName setText:[NSString stringWithFormat:@"%@  Equipment",[m_pDicDetailContact objectForKey:@"kName"]]];
    
    [m_pTableContact reloadData];
}

-(void)onClickBack
{
	[g_AppDelegate goBackHome:self.view];
}


#pragma mark tableViewdelegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    int count = [[m_pDicDetailContact objectForKey:@"kPhone"] count] + [[m_pDicDetailContact objectForKey:@"kEmail"] count];
	return count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    
    // Configure the cell...
    int n = [[m_pDicDetailContact objectForKey:@"kPhone"] count];
    if (indexPath.row < n) {
        cell.textLabel.text = @"Mobile";
        cell.textLabel.textColor = [UIColor colorWithRed:37.0f/255 green:46.0f/255 blue:72.0f/255 alpha:1];
        cell.textLabel.font = [UIFont boldSystemFontOfSize:14];
        
        UILabel *pLblPhone = [[UILabel alloc] initWithFrame:CGRectMake(90, 0, 180, 45)];
        pLblPhone.text = [[m_pDicDetailContact objectForKey:@"kPhone"] objectAtIndex:indexPath.row];
        pLblPhone.textColor = [UIColor colorWithRed:37.0f/255 green:46.0f/255 blue:72.0f/255 alpha:1];
        pLblPhone.backgroundColor = [UIColor clearColor];
        [cell addSubview:pLblPhone];
        [pLblPhone release];
    }
	else {
        cell.textLabel.text = @"Email";
        cell.textLabel.textColor = [UIColor colorWithRed:37.0f/255 green:46.0f/255 blue:72.0f/255 alpha:1];
        cell.textLabel.font = [UIFont boldSystemFontOfSize:14];
        
        UILabel *pLblEmail = [[UILabel alloc] initWithFrame:CGRectMake(90, 0, 180, 45)];
        pLblEmail.text = [[m_pDicDetailContact objectForKey:@"kEmail"] objectAtIndex:indexPath.row - n];
        pLblEmail.textColor = [UIColor colorWithRed:37.0f/255 green:46.0f/255 blue:72.0f/255 alpha:1];
        pLblEmail.backgroundColor = [UIColor clearColor];
        [cell addSubview:pLblEmail];
        [pLblEmail release];
    }

        
	return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    int n = [[m_pDicDetailContact objectForKey:@"kPhone"] count];
    if (indexPath.row < n) {
        NSString *phone = [[m_pDicDetailContact objectForKey:@"kPhone"] objectAtIndex:indexPath.row];
        [self displayComposerSheetSMS:phone];
    }
    else {
        m_pReciver = [[m_pDicDetailContact objectForKey:@"kEmail"] objectAtIndex:indexPath.row - n];
        [self performSelector:@selector(showPicker)];
    }

    
}

////---------------------Email and SMS--------------------------------------//


/////--------------SMS-----------------//
#pragma mark SMS
-(void)displayComposerSheetSMS:(NSString*)phone
{
    MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];
    picker.messageComposeDelegate = self;
    
    picker.recipients = [NSArray arrayWithObject:phone]; // so dien thoai nguoi nhan
    
    [self presentModalViewController:picker animated:YES];
    [picker release];
    NSLog(@"SMS fired");
}

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result {
    
    [self dismissModalViewControllerAnimated:YES];
    
}

//////--------------------Email--------------//
#pragma mark Email
-(void)showPicker
{
	
	Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
	if (mailClass != nil)
	{
		// We must always check whether the current device is configured for sending emails
		if ([mailClass canSendMail])
		{
			[self displayComposerSheetEmail:m_pReciver];
		}
		else
		{
			[self launchMailAppOnDevice];
		}
	}
	else
	{
		[self launchMailAppOnDevice];
	}
}


#pragma mark -
#pragma mark Compose Mail

// Displays an email composition interface inside the application. Populates all the Mail fields. 
-(void)displayComposerSheetEmail:(NSString*)recive 
{
	MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
	picker.mailComposeDelegate = self;
    
    NSArray *toRecipients = [NSArray arrayWithObject:recive];
    [picker setToRecipients:toRecipients];
	
	[self presentModalViewController:picker animated:YES];
    [picker release];
}


// Dismisses the email composition interface when users tap Cancel or Send. Proceeds to update the message field with the result of the operation.
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error 
{	
    [self dismissModalViewControllerAnimated:YES];
}


#pragma mark -
#pragma mark Workaround

// Launches the Mail application on the device.
-(void)launchMailAppOnDevice
{
	
}



/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end
